<?php
//*****************************************************************//
// Pixie: The Small, Simple, Site Maker.                           //
// ----------------------------------------------------------------//
// Licence: GNU General Public License v3                   	   //
// Title: lib_core.                                                //
//*****************************************************************//

// ------------------------------------------------------------------
// class for displaying contents of a db table
	class ShowTable {
	
		var $Res;
	 	var $exclude = array();       
	 	var $table_name;
	 	var $view_number;
	 	var $lo;
	 	var $finalmax;
	 	var $whereami;
	 	var $a_array = array();
	 	var $edit;
	 	
		function ShowTable ($Res,$exclude,$table_name,$view_number,$lo,$finalmax,$whereami,$type,$s) {
	  	$this->Res = $Res;
	    $this->exclude = $exclude;
	    $this->table = $table_name;
	    $this->limit = $view_number;
	    $this->num = $lo;
	    $this->finalmax = $finalmax;
	    $this->whereami = $whereami;
	    $this->page_type = $type;
	    $this->s = $s;
	 	}
	
		function DrawBody () {

			global $date_format, $lang, $page_display_name;
			
			echo "\t<table class=\"tbl $this->table\" summary=\"".$lang['results_from']." $this->table.\">\n\t\t\t\t\t\t\t<thead>\n\t\t\t\t\t\t\t\t<tr>\n"; 
	
	  	for ($j=0; $j < mysql_num_fields($this->Res); $j++) {
	  		if (!in_array(mysql_field_name($this->Res,$j), $this->exclude)) {  
	      	$arlen[$j]=mysql_field_len($this->Res,$j); $sum+=$arlen[$j];
	  		}
	    }
	    
	  	for ($j=0; $j < mysql_num_fields($this->Res); $j++) {
	
		  	if (!in_array(mysql_field_name($this->Res,$j), $this->exclude)) {
		  		$st3="class=\"tbl_heading\"";
	      	$fieldname = simplify(mysql_field_name($this->Res,$j));
	      	if ($lang['form_'.mysql_field_name($this->Res,$j)]) {
	   				$fieldname = $lang['form_'.mysql_field_name($this->Res,$j)];
	      	}
	       	echo "\t\t\t\t\t\t\t\t\t<th $st3 id=\"".mysql_field_name($this->Res,$j)."\">$fieldname</th>\n";
		  	}
		  }
	
	  	echo "\t\t\t\t\t\t\t\t\t<th class=\"tbl_heading\" id=\"page_edit\"></th>\n\t\t\t\t\t\t\t\t\t<th class=\"tbl_heading\" id=\"page_delete\"></th>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t</thead>\n";
	  				  	
	  	if ($this->finalmax) { 
	  		$this->limit = $this->finalmax;
	  	}
	
		  echo "\t\t\t\t\t\t\t<tbody>\n";
	 	  while ($counter < $this->limit) {		
	  		$F = mysql_fetch_array($this->Res);
	  		
	  		if (is_even($counter)) {
	  			$trclass = "odd";
	  		} else {
	  			$trclass = "even";
	  		}	  	
	  		
	  		echo"\t\t\t\t\t\t\t\t<tr class=\"$trclass\">\n";
	  		
	    	for ($j=0; $j < mysql_num_fields($this->Res); $j++) {
	    
	    		if (!in_array(mysql_field_name($this->Res,$j), $this->exclude)) {
		  		 	if (mysql_field_type($this->Res,$j) == "timestamp") {
		  		 		$logunix = returnUnixtimestamp($F[$j]);
		  		 		$date = safe_strftime($date_format, $logunix);
		  		 		echo "\t\t\t\t\t\t\t\t\t<td class=\"tbl_row\" headers=\"".mysql_field_name($this->Res,$j)."\">".$date."</td>\n";
		  		 	} else if (mysql_field_name($this->Res,$j) == "url") {
		  		 		echo "\t\t\t\t\t\t\t\t\t<td class=\"tbl_row\" headers=\"".mysql_field_name($this->Res,$j)."\"><a href=\"".$F[$j]."\">".$F[$j]."</a></td>\n";
		  		 	} else if (mysql_field_name($this->Res,$j) == "email") {
		  		 		echo "\t\t\t\t\t\t\t\t\t<td class=\"tbl_row\" headers=\"".mysql_field_name($this->Res,$j)."\"><a href=\"mailto:".$F[$j]."\">".$F[$j]."</a></td>\n"; 
		  		 	} else if ($F[$j] == "")	{
		  		 		echo "<td class=\"tbl_row\" headers=\"".mysql_field_name($this->Res,$j)."\">No Content</td>\n";
		  		 	} else {
	      	 		echo "\t\t\t\t\t\t\t\t\t<td class=\"tbl_row\" headers=\"".mysql_field_name($this->Res,$j)."\">".strip_tags($F[$j])."</td>\n";
		  		 	}
	     		}
	     	}
	
		    echo "\t\t\t\t\t\t\t\t\t<td class=\"tbl_row tbl_edit\" headers=\"page_edit\"><a href=\"$this->whereami&amp;edit=$F[0]\" title=\"".$lang['edit']."\">".$lang['edit']."</a></td>\n\t\t\t\t\t\t\t\t\t<td class=\"tbl_row tbl_delete\" headers=\"page_delete\"><a href=\"$this->whereami&amp;delete=$F[0]\" onclick=\"return confirm('".$lang['sure_delete_entry']." (#$F[0]) ".$lang['from_the']." $page_display_name ".$lang['settings_page']."?')\" title=\"".$lang['delete']."\">".$lang['delete']."</a></td>\n\t\t\t\t\t\t\t\t</tr>\n";
	 		
	   	$counter++;
	   	}
	   	echo "\t\t\t\t\t\t\t</tbody>\n\t\t\t\t\t\t</table>\n";
	   	} 	   		
	} 
// ------------------------------------------------------------------
// class for add/edit new records in db table
	class ShowBlank {
	
		var $Nam;                      
	 	var $Typ;                      
	 	var $Res;                      
	 	var $Flg;
	 	var $Pkn;                      
		var $edit_exclude = array();				 
		var $table_name;
	
		function ShowBlank ($Nam,$Typ,$Len,$Flg,$Res,$Pkn,$edit_exclude,$table_name) {
	
	  	$this->Nam = $Nam;
	  	$this->Typ = $Typ;
	  	$this->Len = $Len;
	  	$this->Res = $Res;
	  	$this->Flg = $Flg;
	  	$this->Pkn = $Pkn;
	  	$this->exclude = $edit_exclude;
	  	$this->tablename = $table_name;
		}
		
		function ShowBody () {
	 		global $edit, $s, $m, $x, $page, $page_display_name, $lang, $type;
			
			// check $edit against $x - they need to represent the same page, if not redirect.
			$checkid = safe_field('page_id','pixie_core',"page_name='$x'");
			
			if (($edit) and ($m == "static")) {
				if ($edit != $checkid) {
					echo "<div class=\"helper\"><h3>".$lang['help']."</h3><p>".$lang['unknown_edit_url']."</p></div>";
					$cancel = true;
				}
			}
			
			if(!$cancel) {
			
	 		$Nams = explode("|", substr($this->Nam,0,(strlen($this->Nam)-1)));
	 		$Type = explode("|", substr($this->Typ,0,(strlen($this->Typ)-1)));
	 		$Leng = explode("|", substr($this->Len,0,(strlen($this->Len)-1)));
	 		$Flag = explode("|", substr($this->Flg,0,(strlen($this->Flg)-1)));
	 		$Fild = explode("|", substr($this->Res,0,(strlen($this->Res)-1)));
	
		 	if (!$page) { $page = "1"; }

		 	if ($s == "settings") {
		 		if (strpos($this->tablename, "module")) {
		 			$formtitle = $lang['advanced']." ".$lang['page_settings'];
		 		} else if (strpos($this->tablename, "dynamic")) {
		 			$formtitle = $lang['advanced']." ".$lang['page_settings'];
		 		} else {
		 			$formtitle = $lang['page_settings'];
		 		}
		 	} else {
			 	if ($edit) {
					if ($m == "static") {
						$formtitle = $lang['edit']." $page_display_name ".$lang['settings_page'];
					} else {
						$formtitle = $lang['edit']." $page_display_name ".str_replace(".","",$lang['entry'])." (#$edit)";
					}
			 	} else {
			 		$formtitle = $lang['new_entry']." $page_display_name ".str_replace(".","",$lang['entry']);
			 	}
		 	}

			if ($s == "settings") {
				$post = "?s=$s&amp;x=$x";
			} else if ($m == "static") {
				$post = "?s=$s&amp;m=$m&amp;x=$x&amp;edit=$edit&amp;page=$page";
			} else {
				$post = "?s=$s&amp;m=$m&amp;x=$x&amp;page=$page";
			}
	 		echo "<form action=\"$post\" method=\"post\" id=\"form_addedit\" class=\"form\">\n";
	 		echo "\t\t\t\t\t\t<fieldset>\n\t\t\t\t\t\t<legend>$formtitle</legend>\n";
		 	echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"table_name\" value=\"$this->tablename\" maxlength=\"80\" />\n";
	
	 		for ($j=0;$j<count($Nams);$j++) {	
	 		   				
	 				// clears out the form as some of the fields populate
					if (!$edit) { 
		      	$Fild[$j] = "";
		      }
	 				
	      	if (!in_array($Nams[$j], $this->exclude)) { //fields populated and output depending on type etc.
	
	      		//$searchfor = "_".first_word($Nams[$j]);
	      		
	      		if ($Leng[$j]<40) {
	      			$ln=$Leng[$j];
	      		} else if ($Leng[$j]<=400) {
	      			$ln=50;
	      		}

						$nullf = explode(" ",$Flag[$j]);

	   				if ($nullf[0] == "not_null") { // label required fields
	   					if ($lang['form_'.$Nams[$j]]) {
	   						$displayname = $lang['form_'.$Nams[$j]]." <span class=\"form_required\">".$lang['form_required']."</span>";
	   					} else {
	   						$displayname = simplify($Nams[$j])." <span class=\"form_required\">".$lang['form_required']."</span>";
	   					}
	   				} else {
	   					if ($lang['form_'.$Nams[$j]]) {
	   						$displayname = $lang['form_'.$Nams[$j]]." <span class=\"form_optional\">".$lang['form_optional']."</span>";
	   					} else {
	   						$displayname = simplify($Nams[$j])." <span class=\"form_optional\">".$lang['form_optional']."</span>";
	   					}
	   				}

	   				// check language file for any form help
	   				if ($lang['form_help_'.$Nams[$j]]) {
	   					$form_help = "<span class=\"form_help\">".$lang['form_help_'.$Nams[$j]]."</span>";
	   				} else {
	   					$form_help = "";
	   				}

						if ($GLOBALS['rich_text_editor'] == "1") {
	   					$containsphp = strpos(strtolower($Fild[$j]), "<?php");
	   					if ($containsphp) {
	   						$form_help .= " <span class=\"alert\">".$lang['form_php_warning']."</span>";
	   					}
	   				}
		  
	   				echo "\t\t\t\t\t\t\t<div class=\"form_row\">\n\t\t\t\t\t\t\t\t<div class=\"form_label\"><label for=\"$Nams[$j]\">".$displayname."</label>$form_help</div>\n";    //$Type[$j] $Leng[$j] $Flag[$j] for field info
	   				//echo "$Nams[$j] - $Type[$j] - $Leng[$j] - $Flag[$j]"; // see form field properties
						if (($Type[$j] == "timestamp") && (!$edit)) {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
	   					date_dropdown($date);
	   					echo"\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   				} else if (($Type[$j] == "timestamp") && ($edit)) {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
		   				date_dropdown($Fild[$j]);
	   					echo"\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   				//} else if ($Type[$j] == "blob") {
	   				//	echo "\t\t\t\t\t\t\t\t<div class=\"form_item_textarea\">\n\t\t\t\t\t\t\t\t<textarea name=\"$Nams[$j]\" class=\"form_item_textarea_nomce\">$Fild[$j]</textarea>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n"; 	   				
	   				} else if ($Type[$j] == "longtext" || $Leng[$j]>800 || $Type[$j] == "blob") {
	   					if ($GLOBALS['rich_text_editor'] == "1") {
	   						if (!$containsphp) {
	   							echo "\t\t\t\t\t\t\t\t<div class=\"mceSwitch\" id=\"$Nams[$j]_mceSwitch\">\n\t\t\t\t\t\t\t\t\t<input type=\"button\" onclick=\"setTextareaToTinyMCE('$Nams[$j]');\" value=\"Editor\" class=\"mceEditorOn mceCurrent\" /><input type=\"button\" onclick=\"unsetTextareaToTinyMCE('$Nams[$j]');\" value=\"HTML\" class=\"mceEditorOff\" />\n\t\t\t\t\t\t\t\t\t<div class=\"form_item_textarea_mce\">\n\t\t\t\t\t\t\t\t\t\t<textarea name=\"$Nams[$j]\" id=\"$Nams[$j]\" cols=\"50\" class=\"mceEditor\" rows=\"10\">".htmlentities($Fild[$j],ENT_QUOTES,"UTF-8")."</textarea>\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n"; // id=\"$Nams[$j]\"
	   						} else {
	   							echo "\t\t\t\t\t\t\t\t<div class=\"form_item_textarea\">\n\t\t\t\t\t\t\t\t<textarea name=\"$Nams[$j]\" class=\"form_item_textarea_nomce\">$Fild[$j]</textarea>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n"; // id=\"$Nams[$j]\"
	   						}
	   					} else {
	   						echo "\t\t\t\t\t\t\t\t<div class=\"form_item_textarea\">\n\t\t\t\t\t\t\t\t<textarea name=\"$Nams[$j]\" class=\"form_item_textarea_nomce\">$Fild[$j]</textarea>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n"; // id=\"$Nams[$j]\"
	   					}
	   				} else if ($Type[$j] == "set'yes','no'" || $Flag[$j] == "not_null set") {
	   					if ($Fild[$j] == "no") {
	   						echo "\t\t\t\t\t\t\t\t<div class=\"form_item_radio\">\n\t\t\t\t\t\t\t\tYes<input type=\"radio\" name=\"$Nams[$j]\" id=\"$Nams[$j]\" class=\"form_radio\" value=\"yes\" />
	   						     	No<input checked=\"checked\" type=\"radio\" name=\"$Nams[$j]\" class=\"form_radio\" value=\"$Fild[$j]\" />\n\t\t\t\t\t\t\t\t</div>\n\n\t\t\t\t\t\t\t</div>\n";
	   					} else {
		   					echo "\t\t\t\t\t\t\t\t<div class=\"form_item_radio\">\n\t\t\t\t\t\t\t\tYes<input checked=\"checked\" type=\"radio\" name=\"$Nams[$j]\" id=\"$Nams[$j]\" class=\"form_radio\" value=\"yes\" />
	   						     	No<input type=\"radio\" name=\"$Nams[$j]\" class=\"form_radio\" value=\"no\"/>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   					}
	   				} else if (first_word($Nams[$j]) == "image") {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop image_preview\">\n";
	   					db_dropdown("pixie_files", $Fild[$j], $Nams[$j], "file_type = 'Image' order by file_id desc");
	   					echo "\n\t\t\t\t\t\t\t\t<span class=\"more_upload\">or <a href=\"#\" onclick=\"upswitch('".$Nams[$j]."'); return false;\" title=\"".$lang['upload']."\">".strtolower($lang['upload'])."...</a></span>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
						} else if (first_word($Nams[$j]) == "document") {
	   					echo "\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
	   					db_dropdown("pixie_files", $Fild[$j], $Nams[$j], "file_type = 'Other' order by file_id desc");
	   					echo "\n\t\t\t\t\t\t\t\t<span class=\"more_upload\">or <a href=\"#\" onclick=\"upswitch('".$Nams[$j]."'); return false;\" title=\"".$lang['upload']."\">".strtolower($lang['upload'])."...</a></span>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
						} else if (first_word($Nams[$j]) == "video") {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
	   					db_dropdown("pixie_files", $Fild[$j], $Nams[$j], "file_type = 'Video' order by file_id desc");
	   					echo "\n\t\t\t\t\t\t\t\t<span class=\"more_upload\">or <a href=\"#\" onclick=\"upswitch('".$Nams[$j]."'); return false;\" title=\"".$lang['upload']."\">".strtolower($lang['upload'])."...</a></span>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
						} else if (first_word($Nams[$j]) == "audio") {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
	   					db_dropdown("pixie_files", $Fild[$j], $Nams[$j], "file_type = 'Audio' order by file_id desc");
	   					echo "\n\t\t\t\t\t\t\t\t<span class=\"more_upload\">or <a href=\"#\" onclick=\"upswitch('".$Nams[$j]."'); return false;\" title=\"".$lang['upload']."\">".strtolower($lang['upload'])."...</a></span>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
						} else if (first_word($Nams[$j]) == "file") {
	   					echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">\n";
	   					db_dropdown("pixie_files", $Fild[$j], $Nams[$j], "file_id >= '0' order by file_id desc");
	   					echo "\n\t\t\t\t\t\t\t\t<span class=\"more_upload\">or <a href=\"#\" onclick=\"upswitch('".$Nams[$j]."'); return false;\" title=\"".$lang['upload']."\">".strtolower($lang['upload'])."...</a></span>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   				} else if ($Nams[$j] == "tags") {
	   					$tableid = "0";
							$condition = $tableid." >= '0'"; 
							form_tag($this->tablename,$condition);
	   					echo "\t\t\t\t\t\t\t\t<div class=\"form_item\">\n\t\t\t\t\t\t\t\t<input type=\"text\" class=\"form_text\" name=\"$Nams[$j]\" id=\"$Nams[$j]\" value=\"$Fild[$j]\" size=\"$ln\" maxlength=\"".$Leng[$j]."\" />\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   				} else if ($Nams[$j] == "page_blocks") {
	   					form_blocks();
	 	   				echo "\t\t\t\t\t\t\t\t<div class=\"form_item\">\n\t\t\t\t\t\t\t\t<input type=\"text\" class=\"form_text\" name=\"$Nams[$j]\" id=\"$Nams[$j]\" value=\"$Fild[$j]\" size=\"$ln\" maxlength=\"".$Leng[$j]."\" />\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	 	   			} else if ($Nams[$j] == "privs") {
	 	   				if ($Fild[$j] == "2") {
	 	   					$adminclass = "selected=\"selected\"";
	 	   				} else {
	 	   					$everyoneclass = "selected=\"selected\"";
	 	   				}
	 	   				echo"\t\t\t\t\t\t\t\t<div class=\"form_item_drop\">
									<select class=\"form_select\" name=\"$Nams[$j]\" name=\"$Nams[$j]\">
										<option value=\"2\" $adminclass>Administrators only</option>
										<option value=\"1\" $everyoneclass>Administrators &amp; Clients</option>
									</select>
	   							</div>\n\t\t\t\t\t\t\t</div>\n";
	   				} else {
	   					echo "\t\t\t\t\t\t\t\t<div class=\"form_item\">\n\t\t\t\t\t\t\t\t<input type=\"text\" class=\"form_text\" name=\"$Nams[$j]\" id=\"$Nams[$j]\" value=\"$Fild[$j]\" size=\"$ln\" maxlength=\"".$Leng[$j]."\" />\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n";
	   				} 

						//other field types still to come: File uploads...?
	   				
	 				//hidden fields populated
		      } else {
		      	if ((($Nams[$j] == "page_id") && ($s == "publish") && ($m == "dynamic"))) {
		      		$page_id = get_page_id($x);
		      		echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"$page_id\" maxlength=\"".$Leng[$j]."\" />\n";
		      	} else if (last_word($Nams[$j]) == "id") {
		      	 echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"$Fild[$j]\" maxlength=\"".$Leng[$j]."\" />\n";
		      	} else if (($Nams[$j] == "author")) {
		      		if ($edit) {
		      			$output = $Fild[$j];
		      		} else {
		      			$output = $GLOBALS['pixie_user'];
		      		}
		      	 	echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"".$output."\" maxlength=\"".$Leng[$j]."\" />\n";
		      	} else if ($Type[$j] == "timestamp"){
		      		echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"".returnSQLtimestamp(time())."\" maxlength=\"".$Leng[$j]."\" />\n";
		      	} else if ($Nams[$j] == "page_type") {
		      		if ($type) {
		      			$output = $type;
		      		} else {
		      			$output =  safe_field('page_type','pixie_core',"page_id='$edit'");
		      		}
		      		echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"".$output."\" maxlength=\"".$Leng[$j]."\" />\n";
		      	} else if ($Nams[$j] == "publish" && !$edit) {
		      		echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"yes\" maxlength=\"0\" />\n";
		      	} else if ($Nams[$j] == "page_content") {
		      		// do nothing
		      	} else if ($Nams[$j] == "admin") {
		      		// do nothing
						} else {
		      		echo "\t\t\t\t\t\t\t<input type=\"hidden\" class=\"form_text\" name=\"$Nams[$j]\" value=\"$Fild[$j]\" maxlength=\"".$Leng[$j]."\" />\n";
		      	}
		      }
	 		}
	 		
	    if ($edit) {
		  	echo "\t\t\t\t\t\t\t<div class=\"form_row_button\">\n\t\t\t\t\t\t\t\t<input type=\"submit\" name=\"submit_edit\" class=\"form_submit\" value=\"".$lang['form_button_update']."\" />\n\t\t\t\t\t\t\t</div>\n";
	    } else if ($go == "new") {
	    	// do a save draft and save button button?? - when everything can be saved as a draft and is autosaved using AJAX
			} else {
	      echo "\t\t\t\t\t\t\t<div class=\"form_row_button\" id=\"form_button\">\n\t\t\t\t\t\t\t\t<input type=\"submit\" name=\"submit_new\" class=\"form_submit\" value=\"".$lang['form_button_save']."\" />\n\t\t\t\t\t\t\t</div>\n";
	    }
	    if ($m != "static") {
		  	echo "\t\t\t\t\t\t\t<div class=\"form_row_button\">\n\t\t\t\t\t\t\t\t<span class=\"form_button_cancel\"><a href=\"?s=$s&amp;m=$m&amp;x=$x\" title=\"".$lang['form_button_cancel']."\">".$lang['form_button_cancel']."</a></span>\n\t\t\t\t\t\t\t</div>\n";
	    }
	    echo "\t\t\t\t\t\t\t<div class=\"safclear\"></div>\n\t\t\t\t\t\t</fieldset>\n";
	 		echo "\t\t\t\t\t</form>";
		}
	
		}
	} 
// ------------------------------------------------------------------
// build module page
	function admin_module($module_name,$table_name,$order_by,$asc_desc,$exclude=array(NULL),$edit_exclude=array(NULL),$view_number, $tags) { 
	
	  global $type, $go, $page, $message, $s, $m, $x, $edit, $submit_new, $submit_edit, $delete, $messageok, $new, $search_submit, $field, $search_words;
	  
		if ($GLOBALS['pixie_user_privs'] >= 1) {

			$type = "module";

			if ($go == "new") {
				admin_head();
				admin_new($table_name,$edit_exclude);
			} else if ($edit) {
				admin_head();
				admin_edit($table_name,$module_name."_id",$edit,$edit_exclude);
			} else {
				
				admin_carousel($x);
				
				echo "\n\t\t\t\t<div id=\"blocks\">\n";
				admin_block_search($type);
				if ($tags == "yes") {
					admin_block_tag_cloud($table_name, $module_name."_id >= 0");
				}
				echo "\t\t\t\t</div>\n";

				admin_head();
				echo "\t\t\t\t<div id=\"pixie_content\">";
					admin_overview($table_name, '', $order_by, $asc_desc, $exclude, $view_number, $type);
				echo "\t\t\t\t</div>\n";
			}
	  }
	}

// ------------------------------------------------------------------
// display page information
	function admin_head() {
		global $s, $m, $x, $page_display_name, $page_id, $edit, $go, $lang, $tag, $search_words, $search_submit;
		$rs = safe_row("*","pixie_core", "page_name = '$x' limit 0,1");
		if ($rs) {
			extract($rs);
			if ($tag) {
				$stitle = $page_display_name." (".ucwords($lang['all_posts_tagged']).": ".$tag.")";
			} else if ($search_words) {
				$stitle = $page_display_name." (".$lang['search'].": ".chopme(sterilise($search_words),40).")";
			} else if ($search_submit) {
				$stitle = $page_display_name." (".$lang['search'].": $search_submit)";
			} else {
				$stitle = $page_display_name;
			}
			echo "\t\t\t\t<div id=\"page_header\">
					<h2>$stitle</h2>
				</div>\n";	
			if (!$edit) {
				if (!$go) {
				// do not want people to be able to add to comments in this way
				if ($x != "comments") {
				echo "
				<ul id=\"page_tools\">
					<li><a href=\"?s=$s&amp;m=$m&amp;x=$x&amp;go=new\" title=\"".$lang['new_entry']."$page_display_name ".str_replace(".","",$lang['entry'])."\">".$lang['new_entry']."$page_display_name ".str_replace(".","",$lang['entry'])."</a></li>
				</ul>\n";
				}
				}
			}	
		}
	}
// ------------------------------------------------------------------
// display admin block for searching
	function admin_block_search($type) {
		global $s, $m, $x, $lang;
		echo "\n\t\t\t\t\t<div id=\"admin_block_search\" class=\"admin_block\">
			\t\t\t<h3 class=\"$type\">".$lang['search']."</h3>\n";
		echo"\t\t\t\t\t\t<form action=\"?s=$s&amp;m=$m&amp;x=$x\" method=\"post\" id=\"search\">
							<fieldset>
							<legend>".$lang['search']."</legend>
								<div class=\"form_row\">
									<div class=\"form_label\"><label for=\"search-keywords\">".$lang['form_search_words']."</label></div>
									<div class=\"form_item\"><input type=\"text\" name=\"search_words\" id=\"search-keywords\" value=\"\" class=\"form_text\" size=\"25\" /></div>
								</div>							
								<div class=\"form_row_button\">
									<input type=\"submit\" name=\"search_submit\" id=\"search_submit\" value=\"".$lang['search']."\" />
								</div>
							</fieldset>
						</form>";
		echo "\n\t\t\t\t\t</div>\n";
	}
// ------------------------------------------------------------------ 
// view table overview
	function admin_overview($table_name, $condition, $order_by, $asc_desc, $exclude=array(NULL), $view_number, $type)  {
  	global $page, $message, $s, $m, $x, $messageok, $search_submit, $field, $search_words, $tag, $lang;

		$searchwords = trim($search_words);
		
		if ($page) {
			$searchwords = $search_submit;
		}
			
		if ($search_submit) {
			$searchwords = sterilise($searchwords, false);
			//build search sql
			$r2 = safe_query("SHOW fields FROM $table_name");
     	for ($j=0;$j<mysql_num_rows($r2);$j++) {
      	if ($F=mysql_fetch_array($r2)) {
      		$an[$j]= $F['Field'];
      	}
      	if (last_word($an[$j]) != "id") {
      		if($an[$j] != "posted") {
      			if($an[$j] != "author") {
      				if($an[$j] != "comments") {
      					if($an[$j] != "public") {
      						if(first_word($an[$j]) != "last") {
      							if($an[$j] != "date") {
      								$search_sql .= $an[$j]." like '%" . $searchwords . "%' OR ";
      							}
      						}
      					}
      				}
      			}
      		}
      	}
      }
      $search_sql = substr($search_sql,0,(strlen($search_sql)-3))."";
	    //print $search_sql;
		}
		
		$tag = squash_slug($tag);
		if ($search_submit)	{
			if ($m == "dynamic") {
				$page_id = get_page_id($x);
				$r1 = safe_query("select * from $table_name where page_id = '$page_id' and (".$search_sql.") order by $order_by $asc_desc");
			} else {
				$r1 = safe_query("select * from $table_name where ".$search_sql." order by $order_by $asc_desc");
			}
		} else if ($tag) {
			$r1 = safe_query("select * from $table_name where tags REGEXP '[[:<:]]". $tag ."[[:>:]]' order by $order_by $asc_desc");
		} else {
			$r1 = safe_query("select * from $table_name $condition order by $order_by $asc_desc");
		}
		
  	if ($r1) {  

 	 	 	$total = mysql_num_rows($r1);

			if (!isset($page)){
				$lo=0;
				$page=1;

				if ($search_submit)	{
					if ($m == "dynamic") {
						$page_id = get_page_id($x);
						$r = safe_query("select * from $table_name where page_id = '$page_id' and (".$search_sql.") order by $order_by $asc_desc");
					} else {
						$r = safe_query("select * from $table_name where ".$search_sql." order by $order_by $asc_desc");
					}
				} else if ($tag) {
					$r = safe_query("select * from $table_name where tags REGEXP '[[:<:]]". $tag ."[[:>:]]' order by $order_by $asc_desc");
				} else {
					$r = safe_query("select * from $table_name $condition order by $order_by $asc_desc limit $lo,$view_number");
				}
			} else {
				$lo = ($page - 1) * $view_number;
				if ($search_submit)	{
					if ($m == "dynamic") {
						$page_id = get_page_id($x);
						$r = safe_query("select * from $table_name where page_id = '$page_id' and (".$search_sql.") order by $order_by $asc_desc");
					} else {
						$r = safe_query("select * from $table_name where ".$search_sql." order by $order_by $asc_desc");
					}
				} else if ($tag) {
					$r = safe_query("select * from $table_name where tags REGEXP '[[:<:]]". $tag ."[[:>:]]' order by $order_by $asc_desc");				
				} else {	
					$r = safe_query("select * from $table_name $condition order by $order_by $asc_desc limit $lo,$view_number");
				}
			}

			if ($r) {				
				$rows = mysql_num_rows($r);
				$hi = $lo + $view_number; if ($hi > $total) { $finalmax = $total-$lo; $hi = $total; }
				$pages = ceil($total/$view_number);

				if ($pages < 1) {
					$pages = 1;
				}
			}

			$a=&new Paginator_html($page,$total);
   			$a->set_Limit($view_number);
   			$a->set_Links(4);
   			$whereami = "?s=$s&amp;m=$m&amp;x=$x";

			if ($tag) {
				$whereami = "?s=$s&amp;m=$m&amp;x=$x&amp;tag=$tag";
			}

			if ($search_submit) {
				$whereami = "?s=$s&amp;m=$m&amp;x=$x&amp;search_submit=$searchwords";
			}

   		echo "\n\t\t\t\t\t<div class=\"admin_table_holder pcontent\">\n\t\t\t\t\t";
	   	$wheream = "?s=$s&amp;m=$m&amp;x=$x&amp;page=$page";

	   	if ($rows) {
  			$Table = new ShowTable ($r,$exclude,$table_name,$view_number,$lo,$finalmax,$wheream,$type,$s);
    		$Table->DrawBody();

	    	$loprint = $lo + 1;
    		echo "\n\t\t\t\t\t\t<div id=\"admin_table_overview\">\n\t\t\t\t\t\t\t<p>".$lang['total_records'].": $total (".$lang['showing_from_record']." $loprint ".$lang['to']." $hi) $pages ".$lang['page(s)'].".</p>\n\t\t\t\t\t\t</div>\n\n\t\t\t\t\t\t<div id=\"admin_table_pages\">\n\t\t\t\t\t\t\t";
   			echo "<p>"; 
   			$a->previousNext($whereami);
   			echo "</p>";
				echo "\n\t\t\t\t\t\t</div>";
	    	
	   	} else {
	   		if (($search_submit) || ($tag)) {
 	   			echo "<div class=\"helper\"><h3>".$lang['help']."</h3><p>".$lang['helper_search']."</p></div>";
	   		} else {
	   			echo "<div class=\"helper\"><h3>".$lang['help']."</h3><p>".$lang['helper_nocontent']."</p></div>";
	   		}
				echo "\n\t\t\t\t\t</div>\n"; 
			}
			if ($rows) {
				echo "\n\t\t\t\t\t</div>\n";
			}
  	}
	}
// ------------------------------------------------------------------
// show the page carousel
	function admin_carousel($current) {
		global $s, $lang;

		echo "<h2>".$lang['nav2_pages']."</h2>\n";

		$rz = safe_rows("*","pixie_core", "public = 'yes'  and publish = 'yes' order by page_order asc");
		$cc = 1;

		if (count($rz) <= 1) {
				if ($GLOBALS['pixie_user_privs'] >= 2) {
	   			echo "\t\t\t\t<div class=\"helper\"><h3>".$lang['help']."</h3><p>".$lang['helper_nopages404']." ".$lang['helper_nopagesadmin']."</p></div>\n";
				} else {
					echo "\t\t\t\t<div class=\"helper\"><h3>".$lang['help']."</h3><p>".$lang['helper_nopages404']." ".$lang['helper_nopagesuser']."</p></div>\n";
				}
		} else {
		echo "
				<script type=\"text/javascript\">
				$(document).ready(function(){
				  jQuery('#mycarousel').jcarousel({
				    scroll : 1,
				    itemFirstInCallback: {onAfterAnimation: mycarousel_itemFirstInCallback}
				  });
				});
				</script>\n\n";
			
		echo "\t\t\t\t<ul id=\"mycarousel\" class=\"jcarousel-skin-tango\">\n";
		
		$rs = safe_rows("*","pixie_core", "public = 'yes' and in_navigation = 'yes' and publish = 'yes' and page_type != 'plugin' and privs <= '".$GLOBALS['pixie_user_privs']."' order by page_order asc");
		if ($rs) {	
			$num = count($rs);
			$i = 0;
			while ($i < $num){
  			$out = $rs[$i];
  			$page_display_name = $out['page_display_name'];
  			$page_name = $out['page_name'];
  			$page_type = $out['page_type'];
  			$page_id = $out['page_id'];		
  					  			
				$m = $page_type;
				$x = $page_name;

				if ($current == $x) {
					$class = "current";
					$scroll = $cc;
				} else {
					$class ="";
				}
				
	  		if ($m == "dynamic") {
  				echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page innav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_dynamic.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		} else if ($m == "module") {
	  		  echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page innav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_module.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		} else {
	  		  echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page innav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x&amp;edit=$page_id\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_static.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		}
	  	$cc++;
  		$i++;
			}
		}

		$rs = safe_rows("*","pixie_core", "public = 'yes' and in_navigation = 'no' and publish = 'yes' and page_type != 'plugin' and privs <= '".$GLOBALS['pixie_user_privs']."' order by page_name asc");
		if ($rs) {
			$num = count($rs);
			$i = 0;
			while ($i < $num){
  			$out = $rs[$i];
  			$page_display_name = $out['page_display_name'];
  			$page_name = $out['page_name'];
  			$page_type = $out['page_type'];
  			$page_id = $out['page_id'];		
  					  			
				$m = $page_type;
				$x = $page_name;

				if ($current == $x) {
					$class = "current";
					$scroll = $cc;
				} else {
					$class ="";
				}
				
	  		if ($m == "dynamic") {
  				echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page outnav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_dynamic_white.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		} else if ($m == "module") {
	  		  echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page outnav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_module_white.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		} else {
	  		  echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page outnav $class\"><a href=\"?s=$s&amp;m=$m&amp;x=$x&amp;edit=$page_id\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_static_white.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		}
	  		
  		$cc++;
  		$i++;
			}
		}

		$rs = safe_rows("*","pixie_core", "public = 'yes' and in_navigation = 'no' and publish = 'yes' and page_type = 'plugin' and privs <= '".$GLOBALS['pixie_user_privs']."' order by page_order asc");
		if ($rs) {
			$num = count($rs);
			$i = 0;
			while ($i < $num){
  			$out = $rs[$i];
  			$page_display_name = $out['page_display_name'];
  			$page_name = $out['page_name'];
  			$page_type = $out['page_type'];
  			$page_id = $out['page_id'];		
  					  			
				$m = $page_type;
				$x = $page_name;
				
				if ($current == $x) {
					$class = "current";
					$scroll = $cc;
				} else {
					$class ="";
				}				

	  		echo "\t\t\t\t\t<li id=\"c_$page_name\" class=\"page plugin $class\"><a href=\"?s=$s&amp;m=module&amp;x=$x\"><span class=\"page_title\">$page_display_name</span><img src=\"admin/theme/images/icons/page_plugin.png\" alt=\"$m\" class=\"picon\" /></a></li>\n";
	  		
	  	$cc++;
  		$i++;
			}
		}	
		echo "\t\t\t\t</ul>\n";
		}
		// move carousel to current page
		echo "
				<script type=\"text/javascript\">
				function mycarousel_itemFirstInCallback(carousel, item, idx, state)
				{
		    			if (state == 'init') carousel.scroll($scroll, false);
			 	}; 
	  			</script>
	  ";
	}
// ------------------------------------------------------------------
// edit table entry
	function admin_edit($table_name,$edit_id,$edit,$edit_exclude) {
	  global $message, $m, $lang;
	
		$sql = "SELECT * FROM ".$table_name." WHERE ".$edit_id."=".$edit."";
		$r2 = safe_query($sql);
	
		if ($r2) {
	  	if ($F=mysql_fetch_array($r2))  {
	  	for ($j=0;$j<mysql_num_fields($r2);$j++) {
	    	  $an.=mysql_field_name($r2,$j)."|";
	      	$at.=mysql_field_type($r2,$j)."|";
	      	$al.=mysql_field_len($r2,$j)."|";
	      	$af.=mysql_field_flags($r2,$j)."|";
	      	$az.=$F[$j]."|";
	  	}

			if ($m == "static") {
	 			echo "\n\t\t\t\t<div class=\"admin_form form_static\">\n\n\t\t\t\t\t";
			} else {
				echo "\n\t\t\t\t<div class=\"admin_form\">\n\n\t\t\t\t\t";
			}
				
	 		$Blank = new ShowBlank ($an,$at,$al,$af,$az,$nam,$edit_exclude,$table_name);
	 		$Blank->ShowBody();
	
	 		echo "\n\t\t\t\t</div>";
	 		}
		} else {
			$message = $lang['form_build_fail'];
		}
	}
// ------------------------------------------------------------------
// add new table entry
	function admin_new($table_name,$edit_exclude) {

		$an = $at = $af = $az = $al = '';
		
		$r2 = safe_query("show fields from $table_name");	

		for ($j=0;$j<mysql_num_rows($r2);$j++) {
			
		  $r3 = safe_query("select * from $table_name");
		  $flags = mysql_field_flags($r3, $j);
		  $af.= $flags."|";
		  
	    if ($F=mysql_fetch_array($r2)) {
		    $an.= $F['Field']."|";
	    	$at.= ereg_replace("([()0-9]+)","",$F['Type'])."|";
	    }
	    if (ereg ("([0-9]+)", $F['Type'], $str)) {
	    	$al.=$str[0]."|";
	    } else {
		    $al.="|";
		    $az.= $F['Default']."|";
	    }
	    if ($F['Key']=="PRI") {
	    	$nam=$F['Field'];
	    }
		}

	  echo "\n\t\t\t\t<div id=\"admin_form\">\n\n\t\t\t\t\t";  
	
	  $Blank = new ShowBlank ($an,$at,$al,$af,$az,$nam,$edit_exclude,$table_name);
	  $Blank->ShowBody();
	
	  echo "\n\t\t\t\t</div>";
	}
// ------------------------------------------------------------------
// delete code
	if ($GLOBALS['pixie_user'] && $GLOBALS['pixie_user_privs'] >= 1) {
		if ($delete) {
			if (($s == "settings") && ($delete == "1")) {
				// protect 404
			} else if ($s == "settings") {
				$table = "pixie_core";
				$id = "page_id";
			} else if (($s == "publish") && ($m == "dynamic")) {
				$table = "pixie_dynamic_posts";
				$id = "post_id";
			} else if (($s == "publish") && ($m == "module")) {
				$table = "pixie_module_".$x;
				$id = $x."_id";
			}
	
			$getdetails = extract(safe_row("*","$table", "$id='$delete' limit 0,1"));
			if ($getdetails) {
	  		$del = safe_delete("$table", "$id='$delete'");
			}
	
	  	if ($del) {
	  		if (($s == "settings") && ($m == "dynamic")) {
	  			$page_display_name = safe_field('page_display_name','pixie_core',"page_id='$del'");
	  			//do not delete the posts as one false click could destroy lots of data. Backup first?
		  		//safe_delete("pixie_dynamic_posts", "page_id='$delete'"); 
		  		safe_delete("pixie_dynamic_settings", "page_id='$delete'");
	  		}
	
	  		if (($s == "settings") && ($m == "static")) {
		  		$page_display_name = safe_field('page_display_name','pixie_core',"page_id='$del'");
		  		safe_delete("pixie_static_posts", "page_id='$delete'");
	  		}
	
	  		if (($s == "settings") && ($m == "module")) {
	  			$table_mod = "pixie_module_".$page_name;
	  			$table_mod_settings = "pixie_module_".$page_name."_settings";
	  			
		  		$sql = "DROP TABlE IF EXISTS $table_mod";
		  		$sql1 = "DROP TABlE IF EXISTS $table_mod_settings";
		  		
		  		//do not drop the tables as one false click could destroy lots of data. Backup first?
		  		//safe_query($sql);
		  		//safe_query($sql1);
		  		
		  		//do not remove the file as we might want to reinstall at a later date
		  		//file_delete("modules/".$page_name.".php");
	  		}
	
	  		if ($table == "pixie_core") {
		  		$messageok = $lang['ok_delete_page']." ".$page_display_name." ".$lang['page'];
		  		$icon = "site"; 
		  		$alert = "yes";
	  		} else {
	  			$page_display_name = safe_field('page_display_name','pixie_core',"page_name='$x'");
	   	  	$messageok = $lang['ok_delete_entry']." ".$page_display_name." ".$lang['page'];
	   	  	$icon = "page";
	   	  	$alert = "no";
	  		}

				logme($messageok,$alert,$icon);
	
	   	  safe_optimize("$table_name");
				safe_repair("$table_name");
	  	} else {
	  		if (!$message) {
	  			if (($s == "settings") && ($delete == "1")) {
	  				$message = $lang['failed_protected_page'];
	  				$imp = "yes";
	  			} else {
	  				$message = $lang['failed_delete'];
	  				$imp = "no";
	  			}
	  			logme($message,$imp,"error");
	  		}
	    }
		}
	}
// ------------------------------------------------------------------
// save and edit code
	if ($GLOBALS['pixie_user'] && $GLOBALS['pixie_user_privs'] >= 1) {
		
		if (($submit_new) || ($submit_edit)) {

			$rs = safe_row("*","pixie_core", "page_name = '$x' limit 0,1");
    
			if ($rs) {
				extract($rs);
			}
			
			foreach ($_POST as $key=>$value) {
				if (($key == "day") || ($key == "month") || ($key == "year") || ($key == "time")) {
					$value = str_replace(":","",$value);
					$timey[] = $value;
				}
				//echo "$key - $value <br>"; //enable to see $_post output
			}
			
			if ($timey && !checkdate($timey[1],$timey[0],$timey[2])) {
				$error.= $lang['date_error']." |";
			} else {
				$minute = substr($timey[3],2,4);
				$hour = substr($timey[3],0,2);
				$unixtime = mktime($hour, $minute, 00, $timey[1], $timey[0], $timey[2]);
			}

     	$r2 = safe_query("SHOW fields FROM $table_name");
      for ($j=0;$j<mysql_num_rows($r2);$j++) {
      	$r3 = safe_query("select * from $table_name");
		  	$flags = mysql_field_flags($r3, $j);
		  	$af[$j] = $flags;
      	if ($F=mysql_fetch_array($r2)) {
      		$an[$j]= $F['Field'];
      		$at[$j]= ereg_replace("([()0-9]+)","",$F['Type']);
      	}
      	//echo $an[$j]."-".$at[$j]."-".$af[$j]."<br>"; //enable to see field properties      
      }

      for ($j=0;$j<mysql_num_rows($r2);$j++) {
      	$check = new Validator ();
	      if ($at[$j] == "timestamp" && !array_key_exists("$an[$j]",$_POST)) {
	      	$check->validateNumber($unixtime, "invalid time"." |");
					$sql.= "".$an[$j]." = '".returnSQLtimestamp($unixtime)."',";
	      } else if ((last_word($an[$j]) == "id") && (!$had_id)) {
	      	$had_id = "1";
	      	$editid = $_POST[$an[$j]];
	      	$idme = $an[$j];
	      } else if ($an[$j] == "page_content" && $s == "settings") {
	      	//skip it to protect the php in the page_content field
	      } else if ($an[$j] == "admin" && $s == "settings") {
	      	//skip it to protect the php code in the admin field
	      } else {     	
	      	$value = $_POST[$an[$j]];
	      	if ($an[$j] == "title") { $tit = $value; }
	      	if ($at[$j] == "varchar") { $value = htmlentities($value,ENT_QUOTES,"UTF-8"); }

	      	// check for posts with duplicate title/slug and increment
	      	if ($an[$j] == "post_slug") {
	      		if (!$value) {
	      			$value = make_slug($tit);
	      			$value = strtolower($value);
	      			$searchforslug = safe_rows("*", $table_name, "post_slug REGEXP '[[:<:]]". $value ."[[:>:]]'");
	      			if ($searchforslug) {
	      				$addslug = count($searchforslug);
	      				$value = $value."-".$addslug;
	      			}
	      		}
	      	}

	      	// check for pages with duplicate title/slug and increment
	      	if ($an[$j] == "page_name") {
	      		$oldvalue = safe_field('page_name',$table_name,"page_id='$editid'");
	      		if ($value != $oldvalue) {
		      		$searchforpage = safe_rows("*", $table_name, "page_name REGEXP '[[:<:]]". $value ."[[:>:]]'");
		      		if ($searchforpage) {
		      			$addpage = count($searchforpage);
		      			$value = $value."-".$addpage;
		      		}
	      		}
	      		// force the value to be lowercase and without spaces for slug
	      		$value = strtolower(str_replace(" ", "", preg_replace('/\s\s+/', ' ', trim($value))));
	      	}
	      	
	      	// set a page_order, and navigation settings for a newly saved page
	      	if ($an[$j] == "public") {
	      		if ($value == "yes") {
	      			$itspublic = "yes";
	      		}
	      	}
	      	if ($an[$j] == "in_navigation") {
	      		if ($value == "yes") {
	      			$innavigation = "yes";
	      		}
	      	}
	      	if ($an[$j] == "page_order") {	      		
	      		if ($itspublic) {
		      		if ($value != "0") {
		      			if ($innavigation) {
		      				if ($submit_new) {
		      					$value = count(safe_rows("*", $table_name, "public='yes' and in_navigation='yes' order by post_order asc"))+1;
		      				}
		      			} else {
		      				$value = "0";
		      			}
		      		} else {
		      			if ($innavigation) {
		      				$value = count(safe_rows("*", $table_name, "public='yes' and in_navigation='yes' order by post_order asc"))+1;
		      			} else {
		      				$value = $value;
		      			}
		      		}
	      		} else {
		      			$value = "0";
	      		}
	      	}
	      	
	      	// validate and clean input
	      	$nullf = explode(" ",$af[$j]);	      	
	      	if ($an[$j] == "tags") { $value = make_tag($value); }
	      	if (get_magic_quotes_gpc()==0) { $value = addslashes ($value); }
          	if ($at[$j] == "varchar") { sterilise(strip_tags($value)); }
	      	if (($an[$j] == "url") || ($an[$j] == "website")) {
	      		if ($nullf[0] == "not_null") { 
	      			$check->validateURL($value, $lang['url_error']." |" );
	      		} else if ($value != "") { 
	 	      		$check->validateURL($value, $lang['url_error']." |" );
	      		}	
	      	}
	     
		 	if ($at[$j] == "longtext") {
		 		// remove para from <!--more-->
		 		if ($m == "dynamic") {
		 			// hacky to try and clean the more
		 			$value = str_replace("<p><!--more--></p>","<!--more-->",$value);
		 			$value = str_replace("<p> <!--more--></p>","<!--more-->",$value);
		 			$value = str_replace("<!--more--></p>","</p><!--more-->",$value);
		 			$value = str_replace("<p><!--more-->","<!--more--><p>",$value);
		 		}
		 	} 
		 	
	      	if ($an[$j] == "email") {
	      		if ($nullf[0] == "not_null") {
	      			$check->validateEmail($value, $lang['email_error']." |");
	      		} else if ($value != "") {
	      			$check->validateEmail($value, $lang['email_error']." |");
	      		}
	      	}
	      	if (($nullf[0] == "not_null") && ($value == "")) { $error .= ucwords($an[$j])." ".$lang['is_required']." |"; }
	      	$sql.= "`".$an[$j]."` = '".$value."',";

	      	if ($check->foundErrors()) { $error .= $check->listErrors("x"); }
	      	
	      }
      }
      
      $sql = substr($sql,0,(strlen($sql)-1))."";

	    //echo $sql; //view the SQL for current form save

			if (!$error) {
				if ($submit_new) {
					$ok = safe_insert($table_name, $sql);
					safe_optimize($table_name);
					safe_repair($table_name);
					
				  if (!$ok) {
				  	$message = $lang['unknown_error'];
				  	logme($message,"no","error"); 
				  } else {
				  	$idofsave = mysql_insert_id();

						if (($s == "settings") && ($page_type == "dynamic")) {
							$sql = "`page_id` = '$idofsave', `posts_per_page` = '10', `rss` = 'yes'"; 
							safe_insert('pixie_dynamic_settings', $sql);
						}

						if ($table_name == "pixie_core") {
				  		$output = safe_field('page_display_name','pixie_core',"page_id='$idofsave'");
				  		$icon = "site";
				  		$messageok = $lang['saved_new_page'].": $output.";
				  	} else {
				  		$ptitle = $title;
				  		$output = $page_display_name;
				  		$icon = "page";
				  		if ($ptitle) {
				  			$messageok = $lang['saved_new'].": $ptitle ".$lang['in_the']." $output ".$lang['page'];
				  		} else {
				  			$messageok = $lang['saved_new']." (#$idofsave) ".$lang['in_the']." $output ".$lang['page'];
				  		}
				  	}
						
				  	logme($messageok,"no",$icon); 
				  }
				} 

				if ($submit_edit) {
					$ok = safe_update("`$table_name`", "$sql", "`$idme` = '$editid'");

					if (!$ok) {
				  	$message = $lang['unknown_error'];
				  } else {
				  	
				  	if ($s == "settings") {
				  		$output = $page_display_name;
				 			$icon = "site";
					  	if ($output) {
					  		$messageok = $lang['saved_new_settings_for']." ".$output." ".$lang['page'];
					  	} else {
					  		$output = safe_field('page_display_name','pixie_core',"page_id='$page_id'");
					  		$messageok = $lang['saved_new_settings_for']." ".$output." ".$lang['page'];
					  	}
					  	page_order_reset();
				  	}


				    if ($s == "publish") {
				    	$output = $title;
				    	$icon = "page";
				    	$pname = safe_field('page_display_name','pixie_core',"page_id='$page_id'");
				    	if ($m == "static") {
				    		$messageok = "Saved updates to the ".$pname." page.";
				    	} else {
						  	if ($output) {
						  		$messageok = $lang['save_update_entry'].": ".$output." ".$lang['on_the']." ".$pname." ".$lang['page'];
						  	} else {
						  		$messageok = $lang['save_update_entry']." (#".$editid.") ".$lang['on_the']." ".$pname." ".$lang['page'];
						  	}
				    	}
				    }
					 	logme($messageok,"no",$icon); 				  	
				  }
				}					

			} else {
				$err = explode("|",$error);
				$message = $err[0];
			}	
		}
	}
?>